package com.cskaoyan.service;

import com.cskaoyan.bean.BO.OrderDeleteBo;
import com.cskaoyan.bean.BO.OrderRefundBo;
import com.cskaoyan.bean.BO.OrderShipBo;
import com.cskaoyan.bean.PO.*;

import com.cskaoyan.bean.VO.OrderChannelVo;
import com.cskaoyan.bean.VO.OrderDetailVo;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketOrderGoodsMapper;
import com.cskaoyan.mapper.MarketOrderMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @ClassName AdminOrderServiceImpl
 * @Description TODO
 * @Author ke
 * @Date 2022/8/12 17:39
 * @Version 1.0
 */
@Service
public class AdminOrderServiceImpl implements AdminOrderService {

    @Autowired
    MarketOrderMapper marketOrderMapper;

    @Autowired
    MarketOrderGoodsMapper marketOrderGoodsMapper;

    @Autowired
    MarketUserMapper marketUserMapper;


    @Override
    public CommonData<MarketOrder> query(BasePageInfo info, Short[] orderStatusArray, Date start, Date end, String orderSn,Integer userId) {
        MarketOrderExample example = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = example.createCriteria();
        if (start!=null&&end!=null){
            criteria.andAddTimeBetween(start,end);
        }

        // 选择的订单状态是or的关系,所以要用andOrderStatusIn方法
        if (orderStatusArray != null) {
            // List<Short> statusList=new LinkedList<>();
            // for (Short aShort : orderStatusArray) {
            //     statusList.add(aShort);
            // }
            // criteria.andOrderStatusIn(statusList);
            List<Short> statusList = Arrays.asList(orderStatusArray);
            criteria.andOrderStatusIn(statusList);
        }

        if (orderSn != null) {
            criteria.andOrderSnEqualTo(orderSn);
        }

        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }

        criteria.andDeletedEqualTo(false);


        // 在查询前进行分页操作
        PageHelper.startPage(info.getPage(),info.getLimit());

        // 设置排序
        example.setOrderByClause(info.getSort()+" "+info.getOrder());


        List<MarketOrder> marketOrders = marketOrderMapper.selectByExample(example);


        // pageInfo里面包含了页面的信息还有list的信息
        PageInfo pageInfo = new PageInfo(marketOrders);//必须是原始的查询结果



        return CommonData.data(pageInfo);


    }

    @Override
    public Integer delete(OrderDeleteBo bo) {

        Integer orderId = bo.getOrderId();

        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(orderId);

        Short orderStatus = marketOrder.getOrderStatus();

        // 101:未付款 102:用户取消 103:系统取消 201:已付款 202:申请退款 203:已退款 301:已发货 401:用户收货
        // 402:系统收货
        if (orderStatus == 101||orderStatus == 201||orderStatus == 202||orderStatus == 301) {
            return -1;
        }

        MarketOrder marketOrder1 = new MarketOrder();

        marketOrder1.setId(orderId);

        marketOrder1.setDeleted(true);

        marketOrderMapper.updateByPrimaryKeySelective(marketOrder1);

        return 1;
    }

    @Override
    public BaseRespVo detail(Integer orderId) {

        /*
        MarketOrder order;
        List<MarketOrderGoods> orderGoods;
        MarketUser user;
        */
        OrderDetailVo orderDetailVo = new OrderDetailVo();

        // 查询order
        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(orderId);
        orderDetailVo.setOrder(marketOrder);
        // 查询user
        Integer userId = marketOrder.getUserId();
        MarketUser marketUser = marketUserMapper.selectByPrimaryKey(userId);
        orderDetailVo.setUser(marketUser);


        // 查询orderGoods
        MarketOrderGoodsExample example=new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria orderGoodsExampleCriteria = example.createCriteria();
        orderGoodsExampleCriteria.andOrderIdEqualTo(orderId);
        List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByExample(example);
        orderDetailVo.setOrderGoods(marketOrderGoods);


        return  BaseRespVo.ok(0,orderDetailVo,"成功");
    }

    @Override
    public BaseRespVo ship(OrderShipBo bo) {
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId(bo.getOrderId());
        marketOrder.setShipChannel(bo.getShipChannel());
        marketOrder.setShipSn(bo.getShipSn());
        Short status=301;
        marketOrder.setOrderStatus(status);
        marketOrderMapper.updateByPrimaryKeySelective(marketOrder);
        return BaseRespVo.ok(0,null,"成功");
    }

    @Override
    public BaseRespVo channel() {
        LinkedList<OrderChannelVo> orderChannels = new LinkedList<>();

        OrderChannelVo orderChannelVo1 = new OrderChannelVo("ZTO","中通快递");
        OrderChannelVo orderChannelVo2 = new OrderChannelVo("YTO","圆通快递");
        OrderChannelVo orderChannelVo3= new OrderChannelVo("YD","韵达快递");
        OrderChannelVo orderChannelVo4 = new OrderChannelVo("YZPY","邮政快递包裹");
        OrderChannelVo orderChannelVo5 = new OrderChannelVo("EMS","EMS");
        OrderChannelVo orderChannelVo6 = new OrderChannelVo("DBL","德邦快递");
        OrderChannelVo orderChannelVo7 = new OrderChannelVo("FAST","快捷快递");
        OrderChannelVo orderChannelVo8 = new OrderChannelVo("ZJS","宅急送");
        orderChannels.add(orderChannelVo1);
        orderChannels.add(orderChannelVo2);
        orderChannels.add(orderChannelVo3);
        orderChannels.add(orderChannelVo4);
        orderChannels.add(orderChannelVo5);
        orderChannels.add(orderChannelVo6);
        orderChannels.add(orderChannelVo7);
        orderChannels.add(orderChannelVo8);


        return BaseRespVo.ok(0,orderChannels,"成功");
    }

    @Override
    public void refund(OrderRefundBo bo) {
        MarketOrder marketOrder = new MarketOrder();
        Short status=203;
        marketOrder.setId(bo.getOrderId());
        marketOrder.setOrderStatus(status);
        marketOrderMapper.updateByPrimaryKeySelective(marketOrder);
    }
}
